﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany.Forms
{
    public partial class organisation_detail_form : Form
    {
        private eCompany.eObj.organisation m_org = null;
        private eCompany.eObj.currency_list m_cl = null;

        public organisation_detail_form(eCompany.eObj.organisation o, eCompany.eObj.currency_list cl)
        {
            InitializeComponent();
            this.Icon = pics.eCompanyIcon;

            m_org = o;
            m_cl = cl;
            this.Text = m_org.name;
            lblCompanyName.Text = m_org.name;
            LoadImageList();
            RefreshTabs();
            RefreshGeneralTab();
            InitGraph();
        }

        private void LoadImageList()
        {
            foreach (eCompany.eObj.currency cur in m_cl.currencies)
            {
                currencyImageList.Images.Add(cur.short_name, Tools.GetCurrencyImage(cur.short_name));
            }
        }

        private void RefreshTabs()
        {

            companyTabControl.Visible = false;
            foreach (TabPage tp in companyTabControl.TabPages)
            {
                if (tp.Tag.ToString().StartsWith("static") != true)
                    companyTabControl.TabPages.Remove(tp);
            }

            foreach (eObj.account acc in m_org.account_list)
            {
                TabPage tp = new TabPage();
                tp.Text = "Account " + acc.currency;
                tp.Tag = "account" + acc.currency + "Tag";
                tp.BackColor = Color.White;

                account_ctrl ctrl = new account_ctrl(m_org,acc, m_cl);
                ctrl.Top = 7;
                ctrl.Left = 7;
                ctrl.Width = tp.Width - 13;
                ctrl.Height = tp.Height - 13;
                ctrl.Margin = new System.Windows.Forms.Padding(3, 3, 3, 3);
                ctrl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
                tp.Controls.Add(ctrl);

                companyTabControl.TabPages.Add(tp);
                tp.ImageKey = acc.currency;
            }

            companyTabControl.Visible = true;
        }

        private void RefreshGeneralTab()
        {
            eCompany.eManager.eManager eM = new eManager.eManager();

            generalListView.BeginUpdate();
            generalListView.Items.Clear();
            ListViewGroup grp = generalListView.Groups.Add("ACCOUNTS", "Accounts");
            foreach (eCompany.eObj.account acc in m_org.account_list)
            {
                double acc_value = 0;
                double acc_gld_value = 0;
                double old_acc_value = 0;
                double old_gld_value = 0;

                acc_value = eM.getAccountValue(acc);
                old_acc_value = eM.getAccountValue(acc, DateTime.Today.AddDays(-1));
                if (acc.currency != "GLD")
                {
                    acc_gld_value = eM.currencyToGOLD(m_cl, acc.currency, acc_value);
                    old_gld_value = eM.currencyToGOLD(m_cl, acc.currency, old_acc_value);
                }
                else
                {
                    acc_gld_value = acc_value;
                    old_gld_value = old_acc_value;
                }

                ListViewItem item = new ListViewItem();
                item.ImageKey = acc.currency;
                item.Group = grp;
                item.Text = "Account " + acc.currency;
                item.SubItems.Add("");
                item.SubItems.Add(eCompany.eManager.eManager.formatDouble(acc_value)+ acc.currency+
                    " ("+
                    eCompany.eManager.eManager.formatDouble(acc_value-old_acc_value) + ")");
                item.SubItems.Add(eCompany.eManager.eManager.formatDouble(acc_gld_value) +
                    "G"+
                    " ("+eCompany.eManager.eManager.formatDouble(acc_gld_value-old_gld_value)+")");
                generalListView.Items.Add(item);
                generalListView.EndUpdate();
            }
            
            generalListView.SetGroupState(ListViewGroupState.Collapsible);
        }

        public void InitGraph()
        {
            eCompany.eManager.eManager eM = new eManager.eManager();
            List<eCompany.eManager.ChartItem> lst = eM.getOrganisationAccountChartValue(m_org, m_cl);

            System.Windows.Forms.DataVisualization.Charting.Series serie = companyChart.Series[0];
            foreach (eCompany.eManager.ChartItem item in lst)
            {
                serie.Points.AddXY(item.date, item.volume);
            }
        }

        public void refreshGraph()
        {
            companyChart.Series[0].Points.Clear();
            InitGraph();
        }

        private void refreshAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RefreshAll();
        }

        public void RefreshAll()
        {
            RefreshGeneralTab();
            refreshGraph();
            RefreshTabs();
        }

        private void addNewAccountToolStripMenuItem_Click(object sender, EventArgs e)
        {
            eCompany.eManager.eManager eM = new eManager.eManager();
            eCompany.Forms.add_new_account_form f = new add_new_account_form(m_org,m_cl);
            f.ShowDialog();
            RefreshGeneralTab();
            refreshGraph();
            RefreshTabs();
        }

        //private void newAccountToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    eCompany.eManager.eManager eM = new eManager.eManager();
        //    string str_base_value = "";
        //    string cur = newAccountToolStripComboBox.Text;

        //    if (Tools.InputBox("Add new account to company", "Base account value", ref str_base_value) == System.Windows.Forms.DialogResult.OK)
        //    {
        //        System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
        //        str_base_value = str_base_value.Replace(",", ".");
        //        double base_value = Convert.ToDouble(str_base_value, culture);
        //        eM.addAccount(m_org, cur, base_value);
        //        RefreshAll();
        //    }
        //}
    }
}
